package demo.letcode.top100;

import java.util.Arrays;
import java.util.Scanner;

/**
 * 给定一个整数数组和一个目标值，找出数组中和为目标值的两个数。
 * 你可以假设每个输入只对应一种答案，且同样的元素不能被重复利用。
 * 示例:
 * 给定 nums = [2, 7, 11, 15], target = 9
 * 因为 nums[0] + nums[1] = 2 + 7 = 9
 * 所以返回 [0, 1]
 *
 * @author Administrator
 */
public class _1两数之和 {
    public static void main(String[] args) {
        int nums[] = {2, 7, 11, 15};
        System.out.println("已知数组:" + Arrays.toString(nums));
        Scanner sc = new Scanner(System.in);
        System.out.println("输入查找的数:");
        int[] res = twoSum(nums, sc.nextInt());
        System.out.println(Arrays.toString(res));
    }

    public static int[] twoSum(int[] nums, int target) {
        int sum;
        int res[] = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                sum = nums[i] + nums[j];
                if (target == sum) {
                    res[0] = i;
                    res[1] = j;
                    return res;
                }
            }
        }
        return null;
    }
}
